package control;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.tree.MutableTreeNode;

import model.Mp3Data;
import model.NodeFolder;
import model.NodeMp3;
import resources.TestImages;

public class FileIO {
	
	private static int count = 0;

	
	public static MutableTreeNode buildTree(){
		
		// DEMO MODE, otherwise builds a tree from file system...

		Mp3Data testMp3_1 = new Mp3Data("AAA.mp3","BBB","CCC","DDD","1234");
		Mp3Data testMp3_2 = new Mp3Data("aaa.mp3","bbb","ccc","ddd","9999");
		Mp3Data testMp3_3 = new Mp3Data("VVV.mp3","eee","fff","ggg","9999");
		Mp3Data testMp3_4 = new Mp3Data("vvv.mp3","EEE","FFF","GGG","9999");


		NodeMp3 node1 = new NodeMp3(testMp3_1);
		NodeMp3 node2 = new NodeMp3(testMp3_2);
		NodeMp3 node3 = new NodeMp3(testMp3_3);
		NodeMp3 node4 = new NodeMp3(testMp3_4);


		
		MutableTreeNode root = new NodeFolder("ROOT");
		MutableTreeNode folder1 = new NodeFolder("Folder1");
		MutableTreeNode folder2 = new NodeFolder("Folder2");
		MutableTreeNode folder3 = new NodeFolder("Folder3");
		
		root.insert(folder1, 0);
		root.insert(folder2, 1);
		folder2.insert(folder3, 0);
		
		folder1.insert(node1, 0);
		folder3.insert(node2, 0);
		folder1.insert(node3, 1);
		folder3.insert(node4, 1);

		
		return root;
	}
	
	
	public static BufferedImage loadCover(NodeMp3 node){
		BufferedImage bi = null;
		
		
		//DEMO MODE, otherwise retrieves cover art from the mp3 data of the node
		byte[] image;
		if (count % 2 == 0){
			image = TestImages.png;
		}
		else{
			image = TestImages.jpeg;
		}
		
		
		InputStream in = new ByteArrayInputStream(image);
		try {
			bi = ImageIO.read(in);
		} catch (IOException e) {
			System.out.println("IMAGE NOT FOUND: "+node.mp3.getFileName());
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//dummy counter for selecting the other image
		count++;
		return bi;
	}
	
}
